package com.benchmark.springboot.service;

import com.benchmark.springboot.dao.UserDao;
import com.benchmark.springboot.model.User;
import com.power.common.model.CommonResult;
import com.power.common.util.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.SettableListenableFuture;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

/**
 * @author yu 2019/2/16.
 */
@Slf4j
@Service
public class AsyncService {

    @Resource
    private UserDao userDao;

    public void doAsync(String threadId){
        try {
            // 这个方法执行需要三秒
            log.info("执行任务{}开始:{}",threadId, new Date());
            Thread.sleep(500);
            log.info("执行任务{}结束:{}",threadId, new Date());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 无返回异步
     */
    @Async
    public void doNoReturn() {
        try {
            // 这个方法执行需要三秒
            Thread.sleep(3000);
            log.info("方法执行结束:{}", new Date());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 有返回值异步执行
     *
     * @param i
     * @return
     */
    @Async
    public Future<String> doReturn(int i) {
        try {
            // 这个方法需要调用500毫秒
            Thread.sleep(500);
            log.info("执行任务{}:{}", i, new Date());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 消息汇总
        return new AsyncResult<>(String.format("这个是第{%s}个异步调用的证书", i));
    }

    /**
     * 异步数据同步
     *
     * @param userList
     * @param pageIndex
     * @return
     */
    @Async
    public Future<String> syncData(List<User> userList, int pageIndex) {
        log.info("当前数据批次为：{},数据大小为：{}", pageIndex, userList.size());
        Future<String> result = new AsyncResult<>("数据存储成功");
        if (CollectionUtil.isNotEmpty(userList)) {
            try {
                userDao.batchSave(userList);
                Thread.sleep(150);
            } catch (Exception e) {
                log.error("数据存储失败：{}", e);
                result = new AsyncResult<>("数据批次" + pageIndex + "存储失败");
            }
        }
        return result;
    }

    @Async
    public ListenableFuture<CommonResult> findUser(String user) {
        log.info("测试ListenableFuture");
        final SettableListenableFuture<CommonResult> future = new SettableListenableFuture<>();
        future.set(CommonResult.ok());
        // Artificial delay of 1s for demonstration purposes
        try {
            Thread.sleep(1000L);//模拟耗时任务
        } catch (InterruptedException e) {
            log.error("线程整顿啊异常：{}", e);
        }
        return future;
    }
}
